package com.ccx.coachsite.ui.own;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ccx.coachsite.R;
import com.ccx.coachsite.bus.CommonBus;
import com.ccx.coachsite.bus.SoftInfo;
import com.ccx.coachsite.http.BaseResp;
import com.ccx.coachsite.ui.own.OwerMessageView.OnDeleteMessageListener;
import com.ccx.coachsite.ui.own.OwerMessageView.OnSelectAllListener;
import com.ccx.coachsite.ui.own.OwerMessageView.deleteMessageBus;
import com.ccx.coachsite.util.StringUtil;

import android.content.Context;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

public class OwerCollectView extends OwerBaseView {

	private ListView mCollectList;
	private OwerCollectListAdapter mAdapter;

	private ListView mCollectTutorialList;
	private OwerCollectTutorialAdapter mOwerCollectTutorialAdapter;
	private List<SoftInfo> mApp;
	private List<SoftInfo> mAppList;
	private List<SoftInfo> mTutorialList;

	private int currentItemId;
	private int currentIndex;
	
	private View btnDelete;
	private View mEditPanel;
	private boolean isEdit;

	public OwerCollectView(Context context, OwerMainBase superView) {
		super(context, superView);
		loadData();
		
		mEditPanel = findViewById(R.id.ower_collect_edit_panel);
		isEdit = false;
		mEditPanel.setVisibility(View.GONE);
		btnDelete = findViewById(R.id.login_top_right);
		

		mCollectList = (ListView) findViewById(R.id.ower_collect_app_list);
		mCollectTutorialList = (ListView) findViewById(R.id.ower_collect_tutorial_list);

		mCollectList
				.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

					@Override
					public void onCreateContextMenu(ContextMenu menu, View v,
							ContextMenuInfo menuInfo) {
						menu.setHeaderTitle("删除应用收藏");
						menu.add(0, 10000, 0, "删除");

					}
				});

		mCollectTutorialList
				.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

					@Override
					public void onCreateContextMenu(ContextMenu menu, View v,
							ContextMenuInfo menuInfo) {
						menu.setHeaderTitle("删除文章收藏");
						menu.add(0, 10001, 0, "删除");
					}
				});
		
		
		View btnSelectAll = findViewById(R.id.ower_collect_btn_select_all);
		View btnDeleteMessage = findViewById(R.id.ower_collect_btn_delete);
		
		btnSelectAll.setOnClickListener(new OnSelectAllListener());
		btnDeleteMessage.setOnClickListener(new OnDeleteCollectListener());
	}
	
	class OnSelectAllListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			if(mAdapter != null)
			{
				mAdapter.doSelectAll(true);
			}
			if(mOwerCollectTutorialAdapter != null)
			{
				mOwerCollectTutorialAdapter.doSelectAll(true);
			}
		}
	}
	
	private void SetEditInternal(boolean edit)
	{
		isEdit = edit;
		btnDelete.setSelected(isEdit);
		if(isEdit)
		{
			if(mAdapter != null)
			{
				mAdapter.resetList(mAppList);
			}
			if(mOwerCollectTutorialAdapter != null)
			{
				mOwerCollectTutorialAdapter.resetList(mTutorialList);
			}
			mEditPanel.setVisibility(View.VISIBLE);
		}
		else
		{
			mEditPanel.setVisibility(View.GONE);
		}
		
		if(mAdapter != null)
		{
			mAdapter.setEdit(isEdit);
			mAdapter.notifyDataSetChanged();
		}
		if(mOwerCollectTutorialAdapter != null)
		{
			mOwerCollectTutorialAdapter.setEdit(isEdit);
			mOwerCollectTutorialAdapter.notifyDataSetChanged();
		}
	}
	
	public void setEditState()
	{
		if(!isEdit)
		{
			if((mAppList == null || mAppList.size()==0) && (mTutorialList == null || mTutorialList.size()==0))
				return;
		}
		
		SetEditInternal(!isEdit);
	}
	
	class OnDeleteCollectListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			if(mAdapter != null)
			{
				Map<Integer, Boolean> list = mAdapter.getSelectedMap();
				for(int i = mAppList.size() - 1; i>= 0; i--)
				{
					if(list.get(i))
					{
						deleteCollectBus dmb = new deleteCollectBus(mContext);
						dmb.setDeleteCollectParam(mAppList.get(i).getId() + "");
						dmb.setId(10000, mAppList.get(i).getId());
						dmb.setShowTip(false);
						dmb.doTask();
					}
				}
			}
			
			if(mOwerCollectTutorialAdapter != null)
			{
				Map<Integer, Boolean> list = mOwerCollectTutorialAdapter.getSelectedMap();
				for(int i = mTutorialList.size() - 1; i>= 0; i--)
				{
					if(list.get(i))
					{
						deleteCollectBus dmb = new deleteCollectBus(mContext);
						dmb.setDeleteCollectParam(mTutorialList.get(i).getId() + "");
						dmb.setId(10001, mTutorialList.get(i).getId());
						dmb.setShowTip(false);
						dmb.doTask();
					}
				}
			}
		}
	}
	

	public boolean doContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
				.getMenuInfo();
		int index = info.position;
		currentIndex = index;
		currentItemId = item.getItemId();
		int id = -1;
		if (item.getItemId() == 10000) {

			if (index >= 0) {
				id = mAppList.get(index).getId();
			}
		} else if (item.getItemId() == 10001) {
			if (index >= 0) {
				id = mTutorialList.get(index).getId();
			}
		}

		if (id != -1) {
			deleteCollectBus dmb = new deleteCollectBus(mContext);
			dmb.setDeleteCollectParam(id + "");
			dmb.setId(item.getItemId(), id);
			
			dmb.doTask();
		}

		return true;
	}

	protected boolean onLoadData() {
		mApp = OwerProvider.getInstance().getCollectList(mContext);
		if (mApp == null)
			return false;

		mAppList = new ArrayList<SoftInfo>();
		mTutorialList = new ArrayList<SoftInfo>();

		for (int i = 0; i < mApp.size(); i++) {
			if (mApp.get(i).getSoftType() == 0) {
				mAppList.add(mApp.get(i));
			} else {
				mTutorialList.add(mApp.get(i));
			}
		}
		return true;
	}

	protected void doRefreshDataSuccess() {
		if (mAppList != null) {
			mCollectList.setOnItemClickListener(new OnItemClickListener());
			mCollectList.setDivider(mContext.getResources().getDrawable(
					R.drawable.ower_sip_line));
			// mCollectList.setDivider(null);
			mAdapter = new OwerCollectListAdapter(mContext, mAppList);
			mCollectList.setAdapter(mAdapter);
			TextView tview = (TextView) findViewById(R.id.ower_collect_app_count);
			tview.setText(mAppList.size() + "");
		}

		if (mTutorialList != null) {
			mCollectTutorialList
					.setOnItemClickListener(new OnTutorialItemClickListener());
			mCollectTutorialList.setDivider(mContext.getResources()
					.getDrawable(R.drawable.ower_sip_line));
			mOwerCollectTutorialAdapter = new OwerCollectTutorialAdapter(
					mContext, mTutorialList);
			mCollectTutorialList.setAdapter(mOwerCollectTutorialAdapter);
			TextView tview = (TextView) findViewById(R.id.ower_collect_tutorial_count);
			tview.setText(mTutorialList.size() + "");
		}
	}

	private class OnItemClickListener implements
			android.widget.AdapterView.OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> adapter, View v, int pos,
				long arg3) {
			if(mAppList != null && pos < mAppList.size())
			{
				enterApp(mAppList.get(pos).getId());
			}
		}
	}

	private class OnTutorialItemClickListener implements
			android.widget.AdapterView.OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> adapter, View v, int pos,
				long arg3) {
			if(mTutorialList != null && pos < mTutorialList.size())
			{
				enterTutorial(mTutorialList.get(pos).getId(), mTutorialList.get(pos).getName(), mTutorialList.get(pos).getIcon());
			}
		}
	}

	private void doDeleteCollect(int itemId, int softId) {
		if (itemId == 10000) {
			int index = -1;
			for(int i = 0; i < mAppList.size(); i++)
			{
				if(mAppList.get(i).getId() == softId)
				{
					index = i;
					break;
				}
			}
				
			if (index >= 0) {
				mAppList.remove(index);
				mAdapter.notifyDataSetChanged();
				TextView tview = (TextView) findViewById(R.id.ower_collect_app_count);
				tview.setText(mAppList.size() + "");
			}
		} else if (itemId == 10001) {
			int index = -1;
			for(int i = 0; i < mTutorialList.size(); i++)
			{
				if(mTutorialList.get(i).getId() == softId)
				{
					index = i;
					break;
				}
			}
			if (index >= 0) {
				mTutorialList.remove(index);
				mOwerCollectTutorialAdapter.notifyDataSetChanged();
				TextView tview = (TextView) findViewById(R.id.ower_collect_tutorial_count);
				tview.setText(mTutorialList.size() + "");
			}
		}
		
		if((mAppList == null || mAppList.size()==0) && (mTutorialList == null || mTutorialList.size()==0))
		{
			SetEditInternal(false);
		}
	}

	class deleteCollectBus extends CommonBus {
		private int itemId;
		private int softId;
		public void setId(int itemId, int softId)
		{
			this.itemId = itemId;
			this.softId = softId;
		}
		
		public deleteCollectBus(Context context) {
			super(context);
		}

		@Override
		public void callBack(int taskType, BaseResp response,
				String responseJson) {
			super.callBack(taskType, response, responseJson);

			if (response.isSuccess()) {
				doDeleteCollect(itemId, softId);
			} else {

			}
		}
	}
}